//
//  KnowldgeV.m
//  HTT
//  知识点view
//  Created by admin on 15/12/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "KnowldgeV.h"
#import "CourseRecordDetailModel.h"
#import "KnowledgeModel.h"
@interface KnowldgeV()
@property (nonatomic, retain) CourseRecordDetailModel *courseDetailModel;
@property (nonatomic, retain) NSArray *dataArray;
@end

@implementation KnowldgeV
- (id)init{
    self = [super init];
    if (self) {
        [self _initView];
    }
    return self;
}

- (void)_initView{
    [self setBackgroundColor:[UIColor whiteColor]];
}
- (void)setViewData:(id)viewData{
    _courseDetailModel = viewData;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    if (_courseDetailModel.knowledges.count == 0) {
        self.frame = CGRectMake(0, 0, ScreenWidth, 0);
        return;
    }
    _dataArray = [KnowledgeModel mj_objectArrayWithKeyValuesArray:_courseDetailModel.knowledges];
    UILabel *titleLabel = [self getNormolLabel];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.text = @"课程内容";
    titleLabel.textAlignment = _TEXT_ALIGN_LEFT;
    [titleLabel setFrame:CGRectMake(10, 5, ScreenWidth - 20, 30)];
    [self addSubview:titleLabel];
    
    [_dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addBtnIndex:idx];
    }];
}
- (void)addBtnIndex:(NSInteger )index{
    KnowledgeModel *knowledgeModel = [_dataArray objectAtIndex:index];
    UILabel *infoLabel = [self getNormolLabel];
    [infoLabel setText:knowledgeModel.ckInfo];
    [infoLabel setTextAlignment:_TEXT_ALIGN_LEFT];
    [infoLabel setFrame:CGRectMake(10, 41 + index * 21, ScreenWidth - 20 - 30, 30)];
    infoLabel.textColor = [UIColor orangeColor];
    [self addSubview:infoLabel];
    
    UIImageView *moreImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 20, CGRectGetMinY(infoLabel.frame)+5, 10, 20)];
    moreImage.contentMode = UIViewContentModeScaleAspectFill;
    [moreImage setImage:[UIImage imageNamed:@"more"]];
    [self addSubview:moreImage];
    
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(infoLabel.frame) - 1, ScreenWidth, 1)];
    [lineImage setBackgroundColor:RGBCOLOR(230, 230, 230, 1)];
    [self addSubview:lineImage];
    
    UIButton *clearBtn = [[UIButton alloc] initWithFrame:infoLabel.frame];
    [clearBtn setBackgroundColor:[UIColor clearColor]];
    clearBtn.tag = index;
    [clearBtn addTarget:self action:@selector(didSelectKnowledge:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:clearBtn];
    
    [self setFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(clearBtn.frame))];
}
- (void)didSelectKnowledge:(id)sender{
    UIButton *btn  = (UIButton *)sender;
    NSInteger tag = btn.tag;
    DLog(@"%ld",tag);
}
-(UILabel *)getNormolLabel{
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = _TEXT_ALIGN_CENTER;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = RGBCOLOR(0, 0, 0, 0.7);
    label.numberOfLines = 0;
    return label;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
